package com.cmd.user;


import com.alibaba.fastjson.JSONObject;
import com.mgr.UserVoucherMgr;
import com.player.User;
import com.qy.bean.VoucherInfo;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

/**
 * 
 * 发送代金券
 * @author zwl
 * 2018年2月1日
 */
public class VoucherSendCMD  extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		/*
		 * 获取手机号
		 * 获取代金券的id
		 * 如果id的userId匹配则更新用户的代金券发送的手机号
		 */
		JSONObject reJs = new JSONObject();
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		JSONObject param = packet.getMessage();
		if(!param.containsKey("phone")||!param.containsKey("vId")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
			user.sendPacket(reJs);
			return;
		}
		String phone = param.getString("phone");
		int vId = param.getInteger("vId");
		VoucherInfo vInfo = UserVoucherMgr.getUserVoucher(vId);
		if(vInfo==null||vInfo.getUserId()!=user.getID()){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_exist.getType());
			user.sendPacket(reJs);
			return;
		}
		vInfo.setBindPhone(phone);
		UserVoucherMgr.updateUserVoucher(vInfo);
		
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		user.sendPacket(reJs);
		return;
	}

}
